package com.gzhryc.net_marketing.cloud.notice;

import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsCostListRequest;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsCostListResponse;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsUsableCostRequest;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsUsableCostResponse;
import com.gzhryc.net_marketing.api.protocol.order.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;

public class SupplyChainAPIService {

    private static Logger log = Logger.getLogger(SupplyChainAPIService.class);

    private static SupplyChainAPIService me;

    public static SupplyChainAPIService self() {
        if (me == null) {
            me = new SupplyChainAPIService();
        }
        return me;
    }

    private String httpDomain;
    private String token;
    private CloseableHttpClient httpClient;

    public void initial(String httpDomain,String token) {
        this.httpDomain = httpDomain;
        this.token = token;
        this.httpClient = HttpClients.createDefault();
    }

    /**
     * 创建云商品订单
     *
     * @param apiReq
     * @return
     */
    public CreateSupplierGoodsOrderResponse createGoodsOrder(Long supplyId, CreateSupplierGoodsOrderRequest apiReq) {
        CloseableHttpResponse response = null;
        try {
            URI uri = new URIBuilder(httpDomain + "/supplier_api/v1/" + supplyId + "/goods/order/create.do").build();

            StringEntity strEntity = new StringEntity(JsonTools.toJson(apiReq), ContentType.create("application/json", Consts.UTF_8));

            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(strEntity);

            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String str = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                log.info("创建云商品订单返回：" + str);
                if (StringUtils.isNotBlank(str)) {
                    CreateSupplierGoodsOrderResponse apiResp = JsonTools.fromJson(str, CreateSupplierGoodsOrderResponse.class);
                    return apiResp;
                }
            }
        } catch (Exception e) {
            log.error("创建云商品订单错误", e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public GetSupplierGoodsOrderResponse getGoodsOrder(Long supplyId, GetSupplierGoodsOrderRequest apiReq) {
        CloseableHttpResponse response = null;
        try {
            URI uri = new URIBuilder(httpDomain + "/supplier_api/v1/" + supplyId + "/goods/order/get.do").build();

            StringEntity strEntity = new StringEntity(JsonTools.toJson(apiReq), ContentType.create("application/json", Consts.UTF_8));

            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(strEntity);

            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String str = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                log.info("获取云商品订单返回：" + str);
                if (StringUtils.isNotBlank(str)) {
                    GetSupplierGoodsOrderResponse apiResp = JsonTools.fromJson(str, GetSupplierGoodsOrderResponse.class);
                    return apiResp;
                }
            }
        } catch (Exception e) {
            log.error("获取云商品订单错误，" + e.getMessage(), e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public UpdateGoodsOrderAddressResponse updateGoodsOrderAddress(Long supplyId, UpdateGoodsOrderAddressRequest apiReq) {
        CloseableHttpResponse response = null;
        try {
            URI uri = new URIBuilder(httpDomain + "/supplier_api/v1/" + supplyId + "/goods/order/address/update.do").build();

            StringEntity strEntity = new StringEntity(JsonTools.toJson(apiReq), ContentType.create("application/json", Consts.UTF_8));

            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(strEntity);

            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String str = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                log.info("修改云商品订单收货地址返回：" + str);
                if (StringUtils.isNotBlank(str)) {
                    UpdateGoodsOrderAddressResponse apiResp = JsonTools.fromJson(str, UpdateGoodsOrderAddressResponse.class);
                    return apiResp;
                }
            }
        } catch (Exception e) {
            log.error("修改云商品订单收货地址错误，" + e.getMessage(), e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public LogisticsCostListResponse findLogisticsCost(Long supplyId, LogisticsCostListRequest apiReq) {
        CloseableHttpResponse response = null;
        try {
            URI uri = new URIBuilder(httpDomain + "/supplier_api/v1/" + supplyId + "/logistics/cost/find.data").build();

            StringEntity strEntity = new StringEntity(JsonTools.toJson(apiReq), ContentType.create("application/json", Consts.UTF_8));

            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(strEntity);

            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String str = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                log.info("获取物流收费列表返回：" + str);
                if (StringUtils.isNotBlank(str)) {
                    LogisticsCostListResponse apiResp = JsonTools.fromJson(str, LogisticsCostListResponse.class);
                    return apiResp;
                }
            }
        } catch (Exception e) {
            log.error("获取物流收费列表错误，" + e.getMessage(), e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public LogisticsUsableCostResponse getLogisticsUsableCost(Long supplyId, LogisticsUsableCostRequest apiReq) {
        CloseableHttpResponse response = null;
        try {
            URI uri = new URIBuilder(httpDomain + "/supplier_api/v1/" + supplyId + "/logistics/cost/usable.data").build();

            StringEntity strEntity = new StringEntity(JsonTools.toJson(apiReq), ContentType.create("application/json", Consts.UTF_8));

            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(strEntity);

            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String str = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                log.info("获取可用物流收费返回：" + str);
                if (StringUtils.isNotBlank(str)) {
                    LogisticsUsableCostResponse apiResp = JsonTools.fromJson(str, LogisticsUsableCostResponse.class);
                    return apiResp;
                }
            }
        } catch (Exception e) {
            log.error("获取可用物流收费错误，" + e.getMessage(), e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
